package com.sky.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.pay.dao.PayDao;
import com.sky.pay.service.PayService;
import com.sky.pay.vo.Pay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangsk
 */
@Service
public class PayServiceImpl extends ServiceImpl<PayDao, Pay> implements PayService {
    @Autowired
    private PayDao payDao;
    /**
     * 微信给出解密后的数据样例
     * {
     *     "transaction_id":"1217752501201407033233368018",
     *     "amount":{
     *         "payer_total":100,
     *         "total":100,
     *         "currency":"CNY",
     *         "payer_currency":"CNY"
     *     },
     *     "mchid":"1230000109",
     *     "trade_state":"SUCCESS",
     *     "bank_type":"CMC",
     *     "promotion_detail":[
     *         {
     *             "amount":100,
     *             "wechatpay_contribute":0,
     *             "coupon_id":"109519",
     *             "scope":"GLOBAL",
     *             "merchant_contribute":0,
     *             "name":"单品惠-6",
     *             "other_contribute":0,
     *             "currency":"CNY",
     *             "stock_id":"931386",
     *             "goods_detail":[
     *                 {
     *                     "goods_remark":"商品备注信息",
     *                     "quantity":1,
     *                     "discount_amount":1,
     *                     "goods_id":"M1006",
     *                     "unit_price":100
     *                 },
     *                 {
     *                     "goods_remark":"商品备注信息",
     *                     "quantity":1,
     *                     "discount_amount":1,
     *                     "goods_id":"M1006",
     *                     "unit_price":100
     *                 }
     *             ]
     *         },
     *         {
     *             "amount":100,
     *             "wechatpay_contribute":0,
     *             "coupon_id":"109519",
     *             "scope":"GLOBAL",
     *             "merchant_contribute":0,
     *             "name":"单品惠-6",
     *             "other_contribute":0,
     *             "currency":"CNY",
     *             "stock_id":"931386",
     *             "goods_detail":[
     *                 {
     *                     "goods_remark":"商品备注信息",
     *                     "quantity":1,
     *                     "discount_amount":1,
     *                     "goods_id":"M1006",
     *                     "unit_price":100
     *                 },
     *                 {
     *                     "goods_remark":"商品备注信息",
     *                     "quantity":1,
     *                     "discount_amount":1,
     *                     "goods_id":"M1006",
     *                     "unit_price":100
     *                 }
     *             ]
     *         }
     *     ],
     *     "success_time":"2018-06-08T10:34:56+08:00",
     *     "payer":{
     *         "openid":"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"
     *     },
     *     "out_trade_no":"1217752501201407033233368018",
     *     "AppID":"wxd678efh567hg6787",
     *     "trade_state_desc":"支付成功",
     *     "trade_type":"MICROPAY",
     *     "attach":"自定义数据",
     *     "scene_info":{
     *         "device_id":"013467007045764"
     *     }
     * }
     * */
    @Override
    public void createPayRecord(String plainText,String orderNo) {
        HashMap<String,Object> hashMap = JSONObject.parseObject(plainText, HashMap.class);
        /**
         * 生成商家和微信的交易记录，可以根据自身需求参照样例中的字段生成交易记录
         */
        //交易号
        String transactionId = (String)hashMap.get("transaction_id");
        //交易状态
        String tradeState = (String)hashMap.get("trade_state");
        //交易类型
        String tradeType = (String)hashMap.get("trade_type");
        //金额
        Map<String,Object> amountMap = (Map)hashMap.get("amount");
        String amount = (String)amountMap.get("payer_total");
        Date date = new Date();

        Pay pay = new Pay();
        pay.setOrderNo(orderNo);
        pay.setTransactionId(transactionId);
        pay.setTradeStatus(tradeState);
        pay.setTradeType(tradeType);
        pay.setPayTotal(new BigDecimal(amount));
        pay.setContent(plainText);
        pay.setCreateTime(date);
        pay.setUpdateTime(date);
        payDao.insert(pay);
    }
}
